package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Admin;
import com.gaohe.pojo.Tags;
import com.gaohe.service.TagsService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("tags")
public class TagsController {
    @Autowired
    TagsService tagsService;

    @Autowired
    RedisTemplate redisTemplate;

//    分页
    @GetMapping("/page")
    private R page(@RequestParam Integer page, @RequestParam Integer pageSize, HttpSession httpSession, @RequestParam(required = false) String name)
    {
        Page<Tags>page1 = new Page<>();
        LambdaQueryWrapper<Tags>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Tags::getName,name);
        lambdaQueryWrapper.orderByDesc(Tags::getUpdateTime);
        lambdaQueryWrapper.orderByAsc(Tags::getName);
        return R.success(tagsService.page(page1,lambdaQueryWrapper));
    }

//    新建标签
    @PostMapping
    private R save(@RequestBody Tags tags, HttpSession httpSession)
    {
        tags.setCreateTime(LocalDateTime.now());
        tags.setUpdateTime(LocalDateTime.now());
//        Long tags1 = (Long)httpSession.getAttribute("user");
        Long tags1 = (Long)redisTemplate.opsForValue().get("auth");
        tags.setCreateUser(tags1);
        tags.setUpdateUser(tags1);
//        清理缓存
        redisTemplate.delete("tagslist");
        boolean flag = tagsService.save(tags);
        return flag?R.success("添加成功"):R.error("添加失败");
    }
//    查询所有
    @GetMapping
    private R list()
    {
//       1.根据key，从redis中查询
//        Object object = redisTemplate.opsForValue().get("tagslist");
//       2.判断是否为空，如果为空则从mySql中查询，再放入redis中
//        List<Tags>list = new ArrayList<>();
//        if(!StringUtils.isEmpty(object))
//        {
//            list = (List<Tags>) object;
//        }else{
//            list = tagsService.list();
////            放入redis
//            redisTemplate.opsForValue().set("tagslist",list);
//        }
        return R.success(tagsService.list());

    }

//    获取id
    @GetMapping("{id}")
    private R getId(@PathVariable Long id)
    {
        return R.success(tagsService.getById(id));
    }

//    修改标签
    @PutMapping
    private R update(@RequestBody Tags tags)
    {
        System.out.println(tags);
        tags.setUpdateTime(LocalDateTime.now());
        boolean flag = tagsService.updateById(tags);
        System.out.println(tags);
        return flag?R.success("修改成功"):R.error("修改失败");
    }

//    删除标签
    @DeleteMapping
    private R deleted(Long id)
    {
//        这里如果用long传过来的数据不精确
//        必须是大写的Long数据才准确
//        System.out.println(id);
        boolean flag = tagsService.removeById(id);
        return flag?R.success("删除成功"):R.error("删除失败");
    }



}
